<?php
namespace Home\Controller;
use Think\Controller;
class CategoryController extends BaseController {
	public function topcate(){
		$category = M('category');
		$count 	= $category->count();
    	$page_class = new \Think\Page($count, 10);
		$pageshow = $page_class->show(); // 分页显示输出
		$list = $category->select();
		$this->assign('list' , $list); // 赋值数据集
		$this->assign('page' , $pageshow); // 赋值分页输出
		$this->display();
	}

	public function topcate_do(){
		$category = M('category');
		$data['catename'] = I('post.catename', '');
		$data['cateparent'] = I('post.cateparent', '');
		$data['catesort'] = I('post.catesort', '');
		if($category->add($data)) {
			$this->success('添加分类成功', '/Category/topcate');
		} else {
			$this->error($mod_user->getError(), '/Category/topcate');
		}
	}

	public function secate_edit(){
		$category = M('category');
		$id = I('get.id', 0, 'intval');
    	$where['cateid'] = array('eq', $id);
    	$topcate = $category->where($where)->select();
    	$this->assign('topcate', $topcate);
		$this->display();
	}

	public function secate_do(){
		$category = M('category');
		$where['cateid'] = I('post.cateparent', '');
		$data['catename'] = I('post.catename', '');
		$data['catesort'] = I('post.catesort', '');
		$data['cateparent'] = I('post.cateparent', '');
		$topcate['catehaschild'] = I('post.topcatehaschild', '');
		if($category->add($data)) {
			$topcate = $category->where($where)->save($topcate);
			if($topcate){
				$this->success('添加分类成功', '/Category/topcate');
			}else{
				$this->error($category->getError(), '/Category/topcate');
			}
		} else {
			$this->error($category->getError(), '/Category/topcate');
		}
	}

	public function catelist(){
		$cateobj = M('category');
        $caterow = $cateobj->order(array('cateparent'=>'asc'))->select();//查询数据时对parent父级排序
        $treeobj = new \Org\Util\Tree;//引用Tree类
        $row = $treeobj->getTree($caterow,$pid = 0, $col_id = 'cateid', $col_pid = 'cateparent', $col_cid = 'catehaschild');//$col_id,$col_pid,$col_cid对应分类表category中的字段
        $this->assign('cateRow', $row);
        $this->display();
    }
}